<?php

namespace App\Services\Login;

use App\Utils\LoggerUtil;
use App\Utils\ToolUtil;

class GoogleLoginService
{
    private static string $ClientID = "875295397092-16esqt4k9cam5ev3nhini831leangjdh.apps.googleusercontent.com";
    // private static string $ClientSecret = "AIzaSyAUkZqpVY_ie7uxarduwTqflmuAazk87o8";
    // private static string $callBack = "http://api.test.d.ayouqing.com/app/v1/user/login.php";

    /**
     * 验证token，通过token获取用户信息
     * @param $accessToken
     * @return false|mixed
     */
    public static function getGoogleInfo($accessToken)
    {
        try {
            $q = "https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=" . $accessToken;
            $json = ToolUtil::api_curl($q, null);
            // logInfo("google_user_info", $json);
            $userInfoArray = json_decode($json, true);
            if (isset($userInfoArray['aud']) && $userInfoArray['aud'] == self::$ClientID && isset($userInfoArray['sub'])) {
                return ['googleId' => $userInfoArray['sub'], 'name' => $userInfoArray['name']];
            }
            return [];
        } catch (\Throwable $e) {
            LoggerUtil::write("google_user_info", $e->getFile() . ':(' . $e->getLine() . ')@' . $e->getMessage());
            return [];
        }

    }

}